from object import Object
from eventServer import eventServer
from camera import Camera
from cube import Cube
from vec3 import Vec3
from level import Level
from vec2 import Vec2
from inputMap import InputMap
from menu import Menu
from collisionServer import collisionServer
from humanCube import HumanCube
from fsm import FSM
from keys import keys
from gameServer import gameServer
from inputMap import InputMap
from menuServer import menuServer
from particleServer import particleServer

class Acce(Object):
    def __init__(self):
        Object.__init__(self)
        
        eventServer.register("startAcce",self.startAcce)
        eventServer.register('keyDown',self.keyDown)
        
        eventServer.register('pause',self.pause)
        eventServer.register('unpause',self.unpause)
        
        eventServer.register('endConfigInput',self.endConfigInput)
        
    def endConfigInput(self, cmd, args):
        player = args['spinner_player'] - 1
        inputMapName = 'inputMap%d' % player
        inputMap = gameServer.getObject(inputMapName)
        if inputMap and args['accept']:
            if args['input_jump']:
                self.removeInput(inputMap,'jump')
                inputMap.setVar(args['input_jump'],'jump')
            if args['input_moveLeft']:
                self.removeInput(inputMap,'walkLeft')
                inputMap.setVar(args['input_moveLeft'],'walkLeft')
            if args['input_moveRight']:
                self.removeInput(inputMap,'walkRight')
                inputMap.setVar(args['input_moveRight'],'walkRight')
            if args['input_pause']:
                self.removeInput(inputMap,'pause')
                inputMap.setVar(args['input_pause'],'pause')
            if args['input_drop']:
                self.removeInput(inputMap,'drop')
                inputMap.setVar(args['input_drop'],'drop')
            if args['input_goFast']:
                self.removeInput(inputMap,'goFast')
                inputMap.setVar(args['input_goFast'],'goFast')
            
            #if args['input_itemUse']:
            #    self.removeInput(inputMap,'itemUse')
            #    inputMap.setVar(args['input_itemUse'],'itemUse')
            
            inputMap.save(inputMapName + '.yml')
        
        menuServer.popMenu()
        
    def removeInput(self, inputMap, name):
        removeList = []
        for key, val in inputMap.vars.iteritems():
            if val == name:
                removeList.append(key)
        for key in removeList:
            inputMap.vars.pop(key)
        
    def keyDown(self, cmd, args):
        if args['key'] == keys['escape']:
            eventServer.pull('pause',{})
            
    def pause(self, cmd, args):
        if not args.has_key('bin') or args['bin']:
            menuServer.showMenu('pauseMenu')
    
    def unpause(self, cmd, args):
        if not args.has_key('bin') or args['bin']:
            menuServer.popMenu()
        
    def startAcce(self, cmd, args):
        #collisionServer.initialize(25, 0, 0)
        
        
        #self.testCube = gameServer.addObject(gameServer.makeObjectName("cube"),HumanCube(0))
        #self.testCube.setVar("texture", "companionCube.png")
        #self.testCube.setVar("pos",Vec3(5.0,5.0, 0.0))
        
        self.camera = gameServer.addObject("camera",Camera())
        self.level = gameServer.addObject("level",Level())
        
        menuServer.showMenu('mainMenu')
        
